<?php //$Rev:: 58         $ // DO NOT REMOVE

error_reporting(E_ALL);

class Throttle
{

    // hold the users in queue (username => timestamp of last message)
    public $users = array();

    public $core;

    public function __construct(ATAC $object)
    {
        $this->core = $object;
    }

    public function _get($user)
    {
        if (array_key_exists($user, $this->users)) {
            return $this->users[$user];
        }
        return false;
    }

    public function _set($user)
    {
        unset($this->users[$user]);
        $this->users[$user] = time();
        return $this->users[$user];
    }

    public function check($data)
    {
        // admins can't be throttled, duh?
        if (!$this->core->utilities->aim_is_admin($data['screenname'])) {
            if (false !== ($user = self::_get($data['screenname']))) {
                $throt = time() - $user;
                if (in_array($throt, range(0, $this->core->user['throttle_tl']))) {
                    $this->users[$data['screenname']] = time();
                    $this->core->aim_debug('Message throttled from: ' . $data['screenname']);
                    $tl = $throt + $this->core->user['throttle_tl'];
                    $message = 'Your message was dropped because you are exceeding the throttle limit.  Please try again in ' . $tl . ' second(s).';
                    $this->core->utilities->aim_send_im($data['screenname'], $message);
                    return false;
                } else {
                    self::_set($data['screenname']);
                    return true;
                }
            } else {
                // first-time user, add them, but don't throttle
                $this->users[$data['screenname']] = time() - $this->core->user['throttle_tl'];
                return true;
            }
        } else {
            return true;
        }
    }

}

?>